<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动编号" prop="actNo">
        <el-select v-model="queryParams.actNo" placeholder="请输入活动编号" filterable clearable
                   :style="{width: '100%'}">
          <el-option v-for="(item, index) in actNoList" :key="index" :label="item.activityName"
                     :value="item.activityNo" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="活动名称" prop="actName">
        <el-input
          v-model="queryParams.actName"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动开始日期" prop="actBeginDate">
        <el-date-picker v-model="queryParams.actBeginDate" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                        :style="{width: '100%'}" placeholder="活动开始日期" clearable></el-date-picker>
      </el-form-item>
      <el-form-item label="活动结束日期" prop="actEndDate">
        <el-date-picker v-model="queryParams.actEndDate" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                        :style="{width: '100%'}" placeholder="活动结束日期" clearable></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['activity:info:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['activity:info:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-plus"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['activity:info:remove']"
        >报名
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['activity:info:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="活动编号" align="center" prop="actNo"/>
      <el-table-column label="活动名称" align="center" prop="actName"/>
      <el-table-column label="活动开始日期" align="center" prop="actBeginDate"/>
      <el-table-column label="活动结束日期" align="center" prop="actEndDate"/>
      <el-table-column label="活动状态" align="center" prop="actStatus"/>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['student:pupil:query']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['activity:info:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['activity:info:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="活动编号" prop="actNo">
          <el-input :disabled="true" v-model="form.actNo" placeholder="请输入活动编号"/>
        </el-form-item>
        <el-form-item label="活动名称" prop="actName">
          <el-input v-model="form.actName" placeholder="请输入活动名称"/>
        </el-form-item>
        <el-form-item label="活动日期" prop="dateRange">
          <el-date-picker type="daterange" v-model="form.dateRange" format="yyyy-MM-dd"
                          value-format="yyyy-MM-dd" :style="{width: '100%'}" start-placeholder="活动开始日期"
                          end-placeholder="活动结束日期"
                          range-separator="至" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="活动描述" prop="actDesc">
          <el-input v-model="form.actDesc" placeholder="请输入活动描述"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :autosize="{minRows: 4, maxRows: 4}"
                    :style="{width: '100%'}"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 活动详情信息对话框 -->
    <el-dialog :title="title" :visible.sync="detail" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="活动编号" prop="actNo">
          <el-input :disabled="true" v-model="form.actNo"/>
        </el-form-item>
        <el-form-item label="活动名称" prop="actName">
          <el-input v-model="form.actName" readonly clearable/>
        </el-form-item>
        <el-form-item label="活动日期" prop="dateRange">
          <el-date-picker type="daterange" v-model="form.dateRange" format="yyyy-MM-dd"
                          value-format="yyyy-MM-dd" :style="{width: '100%'}" start-placeholder="活动开始日期"
                          end-placeholder="活动结束日期"
                          range-separator="至" clearable readonly clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="活动描述" prop="actDesc">
          <el-input v-model="form.actDesc" readonly clearable/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"
                    readonly clearable/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {gainThreeNoByNo} from "@/api/common";
import {addInfo, delInfo, getInfo, listInfo, updateInfo} from "@/api/activity/info";
import {getValidActInfoList} from "../../api/activity/info";

export default {
  name: "Info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      detail: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        actNo: null,
        actName: null,
        actDesc: null,
        actBeginDate: null,
        actEndDate: null,
        actStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        actNo: [
          {required: true, message: "活动编号不能为空", trigger: "blur"}
        ],
        actName: [
          {required: true, message: "活动名称不能为空", trigger: "blur"}
        ],
        dateRange: [{
          required: true,
          type: 'array',
          message: '请选择活动起止日期',
          trigger: 'change'
        }],
      },
    };
  },
  created() {
    this.getList();
    this.getValidActInfoList();
  },
  methods: {
    /** 查询活动信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getValidActInfoList() {
      getValidActInfoList(this.queryParams).then(response => {
        this.actNoList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.detail = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        actNo: null,
        actName: null,
        actDesc: null,
        actBeginDate: null,
        actEndDate: null,
        actStatus: null,
        remark: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      gainThreeNoByNo("AT").then(response => {
        this.form.actNo = response.data;
        this.open = true;
        this.detail = false;
        this.title = "添加活动信息";
      });
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.form.dateRange = [this.form.actBeginDate, this.form.actEndDate];
        this.open = false;
        this.detail = true;
        this.title = "活动详情信息";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.form.dateRange = [this.form.actBeginDate, this.form.actEndDate];
        this.open = true;
        this.detail = false;
        this.title = "修改活动信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.detail = false;
              this.getList();
            });
          } else {
            this.form.actBeginDate = this.form.dateRange[0];
            this.form.actEndDate = this.form.dateRange[1];
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.detail = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否删除活动:' + row.actName).then(function () {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('activity/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
